package mymyoux.io
{
	import mymyoux.debug.Log;
	import mymyoux.events.MEventDispatcher;
	import mymyoux.events.MPlaylistEvent;
	import mymyoux.events.SynchronizerEvent;
	import mymyoux.io.mserverconnectorClasses.MFilteredPlaylist;
	import mymyoux.io.mserverconnectorClasses.MRoom;
	import mymyoux.io.mserverconnectorClasses.MServerIdentifier;
	import mymyoux.utils.Hash;
	public class Synchronizer extends MEventDispatcher
	{
		static public const PERSISTANT:String="mode_action";
		static public const VOLATILE:String="mode_send";
		protected var _count:uint;
		protected var _room:MRoom;
		protected var _players:MFilteredPlaylist;
		protected var _filters:Hash;
		protected var _peoples:Hash;
		protected var _wasFull:Boolean;
		protected var _name:String;
		protected var players:Hash;
		public function Synchronizer(room:MRoom=null,peoples:Hash=null,filters:Hash=null)
		{
			_wasFull=false;
			this.room=room;
			_count=0;
			this.peoples=peoples;
			this.filters=filters!=null?filters:new Hash();
			players=new Hash();
		}
		public function get peoples():Hash{
			return _peoples;
		}
		public function set peoples(value:Hash):void{
			_wasFull=false;
			_peoples=value;
			if(full){
			//	_wasFull=true;
				dispatchEvent(new SynchronizerEvent(SynchronizerEvent.ALL_PEOPLE_CONNECTED));
			}
		}
		protected function onPeopleIn(e:MPlaylistEvent):void{
			Log.show(e.people +" in");
			players.put(e.people.idx,e.people);
			dispatchEvent(new SynchronizerEvent(SynchronizerEvent.PEOPLE_IN,e.people));
			if(full){
			//	_wasFull=true;
				dispatchEvent(new SynchronizerEvent(SynchronizerEvent.ALL_PEOPLE_CONNECTED));
			}
		}
		protected function get full():Boolean{
			if(_peoples!=null){
				var keys:Array=_peoples.getKeys();
				for(var i:int=0;i<keys.length;i++){
					if(!players.containsKey(keys[i])){
						return false;	
					}
					try{
						if((players.get(keys[i]) as MServerIdentifier).equals(_peoples.get(keys[i]) as MServerIdentifier)){
							return false;
						}
					}catch(e:Error){
						return false;
					}
				}
				return true;
			}
			return false;
		}
		protected function onPeopleInfos(e:MPlaylistEvent):void{
			
		}
		protected function onPeopleOut(e:MPlaylistEvent):void{
			Log.show(e.people +" out");
			players.remove(e.people.idx);
			dispatchEvent(new SynchronizerEvent(SynchronizerEvent.PEOPLE_OUT,e.people));
		}
		public function get room():MRoom{
			return _room;
		}
		public function set room(value:MRoom):void{
			if(_players!=null){
				_players.destroy();
				_players=null;
			}
			_room=value;
			if(_room!=null){
				_players=_room.contacts.filteredPlaylist;
				_players.addEventListener(MPlaylistEvent.PEOPLE_IN,onPeopleIn);
				_players.addEventListener(MPlaylistEvent.PEOPLE_CHANGE_INFO,onPeopleInfos);
				_players.addEventListener(MPlaylistEvent.PEOPLE_OUT,onPeopleOut);
			}
		}
		public function get count():uint{
			return _count;
		}
		public function get filters():Hash{
			return _players!=null?_players.filters:_filters;
		}
		public function set filters(value:Hash):void{
			if(_players!=null){
				_players.filters=value;
			}
			_filters=value;
		}
		public function exec():void{
			Log.show("count"+_count);
				_count++;
				_peoples=players.clone();
				if(_name==null){
					//nom unique
					_name="count"+room.contacts.me.idx+"-"+new Date().time.toString().substring(5)+Math.round(Math.random()*1000);
				}
				Log.show(_name);
				
		}
		public override function destroy():void{
			if(_players!=null){
				_players.destroy();
			}
			
			super.destroy();		
		}
	}
}